Conversation
02.calendar/calendar.rb
Outdated
| end | ||
|
|
||
| result_calendar = WorkClendar.new | ||
| result_calendar.calendar No newline at end of file |
|
|
||
| class WorkClendar | ||
| def initialize() | ||
| opt = OptionParser.new |
02.calendar/calendar.rb
Outdated
| opt = OptionParser.new | ||
|
|
||
| opt.on('-y') {|v| v} | ||
| opt.on('-m') {|v| v} |
There was a problem hiding this comment.
何を参考にして書きました? これではブロックを利用する意味ないかなと思います。
There was a problem hiding this comment.
こちらは当時使い方を理解し切れておらず公式のリファレンスをそのまま参考にした気がします。。
手元に社用PCがないので、明日修正します!
02.calendar/calendar.rb
Outdated
| opt.on('-m') {|v| v} | ||
|
|
||
| @request = opt.parse!(ARGV) | ||
| @request = ARGV |
There was a problem hiding this comment.
これではあまり効果的にクラスやインスタンス変数を使えてないように見えますね。なぜこのような設計にしたんでしょう?
- WorkCalendarというクラス名ですが、Workってどこから出てきた名前だろう?
- @requestという変数はどういう意図の命名だろう? 中身はyearとmonthの配列なのにrequest?
- parse!は ARGVを破壊的に変更するし、ARGVはどこからでもアクセス出来るからそもそも変数にいれる意味あるのだろうか?
などが気になります。
There was a problem hiding this comment.
全体的に変数の命名の仕方に問題がありそうですね。
こうみると確かに自分にしかわからないような変数になっているのがよくわかります😢
実務に入る前に直していけるようにします!以下、変数などの命名理由です。(少し前に行ったものなので記憶があやふやなところもあります)
Workはカレンダーの課題なので、カレンダーワークというイメージからWorkとつけました。😢
リクエストの意図は確か、日にちを指定できるという仕様があったからだったと思います。。
parse!については、初めて使うものだったということもあり、リファレンスあたりを参考に書いた気がします。。
There was a problem hiding this comment.
Workはカレンダーの課題なので、カレンダーワークというイメージからWorkとつけました。😢
既に修正されたようですが、課題だからというのはクラス名の命名に盛り込むのはおかしいかなと思います。あくまでどんなクラスなのか?をベースに考えましょう。このへんは後半の課題でオブジェクト指向に関するものがあるのでそこで改めて学びましょう。
リクエストの意図は確か、日にちを指定できるという仕様があったからだったと思います。
なるほど。ただ、requestだけだと日にちが入ってるようには全く見えないので、もう少し工夫したほうがよかったかなと思います。
parse!については、初めて使うものだったということもあり、リファレンスあたりを参考に書いた気がします。
リファレンスを参照するのは大事なのでいいですね 👍 その上で書いてある内容を理解して使うようにするとなおよいと思います 😄
02.calendar/calendar.rb
Outdated
| @request = ARGV | ||
| end | ||
|
|
||
| def calendar |
There was a problem hiding this comment.
メソッドは原則動詞から始まる命名にするのがセオリーです。このメソッドは何をする役割でしょう? その部分を命名に盛り込みたいですね。
02.calendar/calendar.rb
Outdated
|
|
||
| #日にちを取得 | ||
| designated_date = Date.new(default_year.to_i, default_month.to_i, 1) | ||
| month_days = ((designated_date >> 1) - 1).day |
There was a problem hiding this comment.
月末の日を取得したいだけですよね。
https://bootcamp.fjord.jp/practices/194#-3
のプラクティスのヒントを確認しましょうー。もっと簡単に月末を取得する方法が載ってますよ。このコードはちょっとトリッキー過ぎて読みくいです。僕は初見で読めませんでした。
02.calendar/calendar.rb
Outdated
| #日にちを取得 | ||
| designated_date = Date.new(default_year.to_i, default_month.to_i, 1) | ||
| month_days = ((designated_date >> 1) - 1).day | ||
| date = designated_date.wday |
There was a problem hiding this comment.
dateという命名からはDate型のインスタンスを想像するのが普通かなと思います。実際はそうではないので命名変えたいですね。
02.calendar/calendar.rb
Outdated
| end | ||
|
|
||
| #余白を含めた日にちを一週間ごとに分割 | ||
| new_days = days.each_slice(7).to_a |
There was a problem hiding this comment.
なぜnewなのだろうって思いました。既にdays使ってるし...ぐらいの理由だとしたらもっとちゃんとした命名を練りたいところです。
02.calendar/calendar.rb
Outdated
| end | ||
|
|
||
| #日にちを取得 | ||
| designated_date = Date.new(default_year.to_i, default_month.to_i, 1) |
There was a problem hiding this comment.
これはわかりやすい命名なのですが、初日であるということも命名に盛り込まれるとなおわかりやすいんじゃないかなと思いました。いかがでしょうか。
02.calendar/calendar.rb
Outdated
|
|
||
| #一週間ごとに分けた配列をそれぞれくっつけて曜日と一緒に出力。 | ||
| puts ("日 月 火 水 木 金 土") | ||
| new_days = new_days.map{|item| puts item.join(" ")} |
02.calendar/calendar.rb
Outdated
| #!/usr/bin/env ruby | ||
| require "date" | ||
| require "optparse" | ||
| require 'debug' |
02.calendar/calendar.rb
Outdated
| require 'debug' | ||
|
|
||
| class WorkClendar | ||
| class Clendar |
There was a problem hiding this comment.
クラス名については違和感なくなりました。一方でインスタンス変数がゼロになったので、もはやクラス使う意味がないなと思います。したがってクラスなしにしましょうか。クラスについては後半の課題で出てくるのでそのときに改めて学ぶのがいいんじゃないかなと思います。
02.calendar/calendar.rb
Outdated
| @request = opt.parse!(ARGV) | ||
| @request = ARGV | ||
| opt.on('-y') | ||
| opt.on('-m') |
There was a problem hiding this comment.
ブロックなくなりましたが、前回の指摘の意図としてはブロックをうまく活用してほしいな、ということでした。
opt.parse!(ARGV)した後のARGVの中身ってあまり扱いやすいものではない(ARVG[0]やARGV[1]のようなものってリーダブルじゃないですよね)と思いますので。もし難しいならば、
https://docs.ruby-lang.org/ja/latest/method/OptionParser/i/getopts.html
というのもあるのでこちらを使うほうがわかりやすいかなと思います。
02.calendar/calendar.rb
Outdated
| else | ||
| default_month = @request[1] | ||
| target_month = ARGV[1] | ||
| end |
There was a problem hiding this comment.
上で指摘しましたが、getopt使うようにするとこの辺の実装も変わってきそうです。
02.calendar/calendar.rb
Outdated
| month_days = ((designated_date >> 1) - 1).day | ||
| date = designated_date.wday | ||
|
|
||
| first_day_of_designated_date = Date.new(target_year.to_i, target_month.to_i, 1) |
There was a problem hiding this comment.
もはやシンプルにfirst_dayくらいで十分わかるんじゃないですかね。なお、dayとdateは意味が違ってきます。今回どちらがより適切かも考えてみてください。
02.calendar/calendar.rb
Outdated
| date = designated_date.wday | ||
|
|
||
| first_day_of_designated_date = Date.new(target_year.to_i, target_month.to_i, 1) | ||
| month_days = Date.new(target_year.to_i, target_month.to_i, -1).day |
There was a problem hiding this comment.
前回指摘しなかったのですが、複数形の命名は通常は配列だと思ってしまいます。この場合単なる数字なので複数形を使わずに適切な命名を考えてみてほしいです。
02.calendar/calendar.rb
Outdated
|
|
||
| first_day_of_designated_date = Date.new(target_year.to_i, target_month.to_i, 1) | ||
| month_days = Date.new(target_year.to_i, target_month.to_i, -1).day | ||
| get_day_of_week = first_day_of_designated_date.wday |
There was a problem hiding this comment.
get_day_of_weekですと動詞始まりなのでメソッド名のような命名に感じます。変数名は名詞や形容詞+名詞など、動詞を使わない命名をするのが自然だと思います。もう少し練ってみてください 🙏
02.calendar/calendar.rb
Outdated
| if num < 10 | ||
| days.push(" #{num}") | ||
| month_days.times do |n| | ||
| n += 1 |
There was a problem hiding this comment.
1始まりにしたいからこうしてるんですよね? うまく工夫すれば1はじまりでループする方法があると思うんです。+1せずに済むコードにしてみてほしいのですが難しいでしょうか。
02.calendar/calendar.rb
Outdated
| puts first_day_of_designated_date.strftime("%B %Y").center(20) | ||
| puts ("日 月 火 水 木 金 土") | ||
| new_days = new_days.map{|item| puts item.join(" ")} | ||
| week.each{|day| puts day.join(" ")} |
|
動作確認してみると、-yだけ、-mだけ指定したときにエラーが発生します。-yだけのときは月は当月、-mだけのときは年は今年となるような動きにしましょう。 |
02.calendar/calendar.rb
Outdated
| opt.on('-y Integer') { | ||
| |y| option[:y] = y | ||
| target_year = option[:y] | ||
| } |
There was a problem hiding this comment.
ちょっと書き方がおかしいかなーと思います。
- ブロック変数のところは同じ行に書きましょう
- ブロック内が複数行のときは
{ }でなくて、do endを使いましょう
NG
[1,2,3].each {
|i| i
}OK
[1,2,3].each { |i| i }
[1,2,3].each do |i|
# 通常同じ記述を繰り返しませんが、複数行の表現のための便宜上です。
puts i
puts i
endThere was a problem hiding this comment.
option[:y] = y
target_year = option[:y]はoptions[:y]に一旦代入する意図がよくわからなかったです。直接target_yearに入れない理由がなにかあるのでしょうか。
02.calendar/calendar.rb
Outdated
| opt.on('-y Integer') { | ||
| |y| option[:y] = y | ||
| target_year = option[:y] | ||
| } |
There was a problem hiding this comment.
option[:y] = y
target_year = option[:y]はoptions[:y]に一旦代入する意図がよくわからなかったです。直接target_yearに入れない理由がなにかあるのでしょうか。
02.calendar/calendar.rb
Outdated
| if ARGV == [] | ||
| target_month = Date.today.mon | ||
| first_day = Date.new(target_year.to_i, target_month.to_i, 1) | ||
| month_day = Date.new(target_year.to_i, target_month.to_i, -1).day |
There was a problem hiding this comment.
月末の日付のことを英語でmonth dayと表現するのでしたっけ? もしそうならいいのですが、そうでないならおかしいのかな思います。いかがでしょう? いい表現が思いつかないのであれば、first_dayとの対比で
last_day = month_day = Date.new(target_year.to_i, target_month.to_i, -1)とした上で、month_dayの利用箇所をlast_dayを使ったもので書き換えてもよいかな、と思います。
There was a problem hiding this comment.
今更ですが僕の記述が変ですね
last_day = Date.new(target_year.to_i, target_month.to_i, -1)のつもりでした。
02.calendar/calendar.rb
Outdated
| days.push(" ") | ||
| end | ||
| #その月の日数分daysに日にちを追加 | ||
| 1.upto(month_day) do |d| |
02.calendar/calendar.rb
Outdated
| month_day = Date.new(target_year.to_i, target_month.to_i, -1).day | ||
| day_of_week = first_day.wday | ||
|
|
||
| #曜日ぶんから文字を挿入(初日のスタート位置(曜日)をとるため) |
There was a problem hiding this comment.
もしかしたら既に指摘しているかもですが、説明的なコメントは可能な限り避けましょう。
参考
https://qiita.com/jnchito/items/f0d90af4ed44b7484103
とはいえ慣れないうちは難しいかもしれないですし、無理になくさなくても大丈夫です。
02.calendar/calendar.rb
Outdated
| opt.parse!(ARGV) | ||
|
|
||
| first_day = Date.new(target_year.to_i, target_month.to_i, 1) | ||
| last_day = Date.new(target_year.to_i, target_month.to_i, -1).day |
There was a problem hiding this comment.
first_day = Date.new(target_year.to_i, target_month.to_i, 1)
last_day = Date.new(target_year.to_i, target_month.to_i, -1).dayで、first_dayはDate型クラスなのに対して、last_dayは単なるIntegerの値です。これでは対比にならないですね。.dayの呼び出しはlast_dayの参照箇所(一箇所しかない)でやればいいのでは、と思います。
あと、_dayは_dateのほうがもしかしたらわかりやすいかもしれません。Date型だからです。
02.calendar/calendar.rb
Outdated
| end | ||
|
|
||
| 1.upto(last_day) do |d| | ||
| single_digit_date = d < 10 |
There was a problem hiding this comment.
条件判定を変数にしたのですね。この命名ですとbooleanのイメージが湧きにくいかもしれません。こうしたいのであれば、is_single_digit_dateのようなものであるほうがbooleanと伝わりやすいかもしれません。booleanとしてどんな命名がいいかはプログラミング一般の話として検索すると出てくると思います。よくあるのはis_xxx, has_xxx, can_xxx,や、末尾をxxxableのようなものにするなどでしょうか。
Rubyの場合、メソッドであれば?が使えるのでbooleanの値を返すメソッドなら末尾に?をつけるのが一般的ですね。
そしてそもそもを言うと、これくらいならばd < 10で十分読みとけるので命名に悩むくらいならそのまま書いてもいいかな、とは思います。
02.calendar/calendar.rb
Outdated
| opt.parse!(ARGV) | ||
|
|
||
| first_date = Date.new(target_year.to_i, target_month.to_i, 1) | ||
| last_day = Date.new(target_year.to_i, target_month.to_i, -1) |
There was a problem hiding this comment.
first_dateを直したのなら、last_dayも直しましょう。くどいようですが、これでは対比にならない命名ですし、どちらもDate型ですから。
No description provided.